﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using GeometryProcessing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace CCD
{
    public partial class ProcessConsole : Form
    {

        #region GLOBAL VARIABLE

        Image<Rgb, Byte> _imgAve;
        Image<Rgb, Byte> _imgLower;
        Image<Rgb, Byte> _imgUpper;
        Image<Rgb, Byte> _imgDiff;

        Image<Rgb, Byte> _imgProc;

        string _modelPath;

        #endregion

        public ProcessConsole()
        {
            InitializeComponent();
        }

        public ProcessConsole(Image<Rgb, Byte> imgProc, string modelPath)
        {
            InitializeComponent();

            _modelPath = modelPath;
            _imgProc = imgProc;
            _imgAve = new Image<Rgb,byte>(Path.Combine( modelPath,"imgAve.bmp"));
            _imgLower = new Image<Rgb, byte>(Path.Combine(modelPath, "imgMin.bmp"));
            _imgUpper = new Image<Rgb, byte>(Path.Combine(modelPath, "imgMax.bmp"));

        }

        private void ProcConsole_Load(object sender, EventArgs e)
        {

        }

        public double TestBlock(string blockPath)
        {
            // 读取蒙版图片
            string maskFilename = Path.Combine(blockPath, "mask.bmp");
            Image<Gray, Byte> imgMask = new Image<Gray, byte>(maskFilename);
            // 读取配置信息
            string configFilename = Path.Combine(blockPath, "config.ini");
            FileStream fs = new FileStream(configFilename, FileMode.Open, FileAccess.Read);
            StreamReader rd = new StreamReader(fs);
            // 读取rect
            string line = rd.ReadLine();
            string[] rectStr = line.Split(new char[] { ',' });
            int left = int.Parse(rectStr[0]);
            int top = int.Parse(rectStr[1]);
            int width = int.Parse(rectStr[2]);
            int height = int.Parse(rectStr[3]);
            Rectangle rect = new Rectangle(left, top, width, height);
            // 读取阈值
            line = rd.ReadLine();
            double threshold = double.Parse(line);


            // Prepare the result
            var imgRst = new Image<Rgb, Byte>(_imgProc.Size);

            // MAX and MIN
            
            var imgRst1 = _imgLower.Cmp(_imgProc, Emgu.CV.CvEnum.CMP_TYPE.CV_CMP_GT);
            var imgRst2 = _imgUpper.Cmp(_imgProc, Emgu.CV.CvEnum.CMP_TYPE.CV_CMP_LT);
            imgRst = imgRst1.Or(imgRst2);

            // Gray, Morph
            var imgRstGray = imgRst.Convert<Gray, Byte>();
            imgRstGray._ThresholdBinary(new Gray(200), new Gray(255));
            imgRstGray._Erode(3);
            imgRstGray._Dilate(3);
            imgRstGray._And(imgMask);

            // SHow Image
            Image<Rgb, Byte> showImg = imgRst.Clone();
            showImg = (imgRstGray.Convert<Rgb, Byte>()).Mul(0.8).Add(_imgProc.Mul(0.2));

            imageBox1.Image = imgRstGray;

            return 1;
        }
    }
}
